{include file="public/header"/}
<script src="{__PUBLIC_PATH}/common/js/jquery.js?v={$version}"></script>
<script src="{__PUBLIC_PATH}/admin/pcf/global.js?v={$version}"></script>
</head>
<body>
<style>
.layui-table-cell {height:30px!important;}
textarea {padding:5px;}
@media screen and (max-width:800px){
.layui-layer.layui-layer-page{width:100%!important;overflow:hidden!important;left:0!important;}
.layui-layer-title{width:100%!important;box-sizing:border-box;}
.layui-layer-content{width:100%!important;}
}
</style>
<div class="layui-fluid" id="LAY-component-layer-list">
<div class="layui-card">
<div class="layui-card-body table-tool-mini">
<div class="layui-form toolbar">
<div class="layui-form-item">
    <div class="layui-inline">
        <button data-type="add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>新增表单</button>
        <button data-type="batch_del" data-url="{:url('/guestbook/batch_del')}" class="layui-btn layui-btn-danger icon-btn">
        <i class="layui-icon"></i>批量删除</button>
    </div>
</div>
</div>
<table class="layui-table" id="tableUser" lay-filter="tableUser"></table>
</div>
</div>
</div>
<script type="text/html" id="tableBarUser">
<a class="layui-btn layui-btn-primary layui-btn-sm" data-type="edit" data-id="{{d.id}}">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-sm" data-title="{{d.title}}" data-id="{{d.id}}" data-type="del">删除</a>
</script>
<script type="text/html" id="tableState">
<input type="checkbox" name="status" data-id="{{d.id}}" value="{{d.status}}" lay-filter="status" lay-skin="switch" lay-text="正常|锁定" {{d.status == 1 ? 'checked':''}}>
</script>
<script type="text/html" id="tableState1">
<input type="checkbox" name="need_login" data-id="{{d.id}}" value="{{d.need_login}}" lay-filter="need_login" lay-skin="switch" lay-text="是|否" {{d.need_login == 1 ? 'checked':''}}>
</script>
{include file="public/footer"/}
<script>
    layui.use(['layer', 'form', 'table', 'util'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;

        // 渲染表格
        var insTb = table.render({
            elem: '#tableUser',
            url: "{:url('/guestbook/index')}",
            page: true,
            cellMinWidth: 100,
            toolbar: false,
            cols: [[
                {type:'checkbox'},
                {field:'id',title:'ID',width:60,align: 'center'},
                {field: 'title', title: '表单标题'},
                {field: 'attrcount', title: '字段数量',align: 'center',width: 100},
                {field: 'id', title: '表单代码',align: 'center',width: 100,templet: function (d) {
                    return '<a class="layui-btn layui-btn-primary layui-btn-xs" data-type="code" data-id="'+d.id+'">获取代码</a>'; 
                }},
                {field: 'status', templet: '#tableState', title: '状态',align: 'center',width: 100},
                {field: 'need_login', templet: '#tableState1', title: '需要登录',align: 'center',width: 100},
                {field: 'listcount',title: '留言数量',align: 'center',width: 200,templet: function (d) {
                    return '<a class="layui-btn layui-btn-xs layui-btn-normal" href="{:url("/guestbook/bookcontent")}?id='+d.id+'}">内容管理</a> '+d.listcount+'条'; 
                }},
                {toolbar: '#tableBarUser', title: '操作',width: 200,align:'center'}
            ]]
        });

        //开启关闭
        form.on('switch(status)', function(){
            if ($(this).val() == 0){
                $(this).val(1);
            }else{
                $(this).val(0);
            }
            var dataid = $(this).attr('data-id');
            var pcfurl = '{:url("/index/changeTableVal")}';
            changeTableVal('guestbook','id',dataid,'status',this,pcfurl,'/Guestbook/index');
        });

        //开启关闭
        form.on('switch(need_login)', function(){
            if ($(this).val() == 0){
                $(this).val(1);
            }else{
                $(this).val(0);
            }
            var dataid = $(this).attr('data-id');
            var pcfurl = '{:url("/index/changeTableVal")}';
            changeTableVal('guestbook','id',dataid,'need_login',this,pcfurl,'/Guestbook/index');
        });

        $(document).on("click","#LAY-component-layer-list .layui-btn",function(){
          var type = $(this).data('type');
          active[type] && active[type].call(this);
        });

        //触发事件
        var active = {
            add: function(){
                layer.open({
                      type: 2,
                      title: '添加表单',
                      fixed: true,
                      shadeClose: false,
                      shade: 0.3,
                      area: ['100%', '100%'],
                      content: "{:url('/guestbook/add')}"
                });
            },
            edit: function(){
                var obj = this;
                var id = $(obj).attr('data-id');
                layer.open({
                      type: 2,
                      title: '修改表单',
                      fixed: true,
                      shadeClose: false,
                      shade: 0.3,
                      area: ['100%', '100%'],
                      content: "{:url('/guestbook/edit')}?id="+id
                });
            },
            del: function(){
                var obj = this;
                var ntitle = $(obj).attr('data-title');
                var id = $(obj).attr('data-id');
                layer.msg('确定要删除“' + ntitle + '”吗？', {
                    btnAlign: 'c',
                    time: 0,
                    btn: ['确定', '取消'],
                    yes: function(index, layero){
                        layer.close(index);
                        $.get("{:url('/guestbook/del')}", {del_id: id}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 1) {
                                layer.msg(res.msg,{icon: 1,time:1000},function(){
                                    insTb.reload();
                                });
                            } else {
                                layer.msg(res.msg, {icon: 2,time:1000});
                            }
                        }, 'json');
                    }
                });
            },
            batch_del: function(){
                var checkRows = table.checkStatus('tableUser');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', {icon: 2,time:1000});
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                batch_del(this, ids);
            },
            code: function(){
                var obj = this;
                var id = $(obj).attr('data-id');
                parent.layer.open({
                    type: 2,
                    title: '代码调用', 
                    shadeClose: true,
                    area: ['480px', '280px'],
                    content: "{:url('/guestbook/getcode')}?id="+id
                });
            }
        };
    });
</script>
</body>
</html>